/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import ecfdb.Acheteur;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ecfdb.Vente;
import java.util.ArrayList;
import java.util.List;
import ecfdb.Cajot;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author QEMLAB
 */
public class AcheteurJpaController implements Serializable {

    public AcheteurJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Acheteur acheteur) {
        if (acheteur.getVenteList() == null) {
            acheteur.setVenteList(new ArrayList<Vente>());
        }
        if (acheteur.getCajotList() == null) {
            acheteur.setCajotList(new ArrayList<Cajot>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Vente> attachedVenteList = new ArrayList<Vente>();
            for (Vente venteListVenteToAttach : acheteur.getVenteList()) {
                venteListVenteToAttach = em.getReference(venteListVenteToAttach.getClass(), venteListVenteToAttach.getIdvente());
                attachedVenteList.add(venteListVenteToAttach);
            }
            acheteur.setVenteList(attachedVenteList);
            List<Cajot> attachedCajotList = new ArrayList<Cajot>();
            for (Cajot cajotListCajotToAttach : acheteur.getCajotList()) {
                cajotListCajotToAttach = em.getReference(cajotListCajotToAttach.getClass(), cajotListCajotToAttach.getIdcajot());
                attachedCajotList.add(cajotListCajotToAttach);
            }
            acheteur.setCajotList(attachedCajotList);
            em.persist(acheteur);
            for (Vente venteListVente : acheteur.getVenteList()) {
                Acheteur oldAcheteurOfVenteListVente = venteListVente.getAcheteur();
                venteListVente.setAcheteur(acheteur);
                venteListVente = em.merge(venteListVente);
                if (oldAcheteurOfVenteListVente != null) {
                    oldAcheteurOfVenteListVente.getVenteList().remove(venteListVente);
                    oldAcheteurOfVenteListVente = em.merge(oldAcheteurOfVenteListVente);
                }
            }
            for (Cajot cajotListCajot : acheteur.getCajotList()) {
                Acheteur oldAcheteurOfCajotListCajot = cajotListCajot.getAcheteur();
                cajotListCajot.setAcheteur(acheteur);
                cajotListCajot = em.merge(cajotListCajot);
                if (oldAcheteurOfCajotListCajot != null) {
                    oldAcheteurOfCajotListCajot.getCajotList().remove(cajotListCajot);
                    oldAcheteurOfCajotListCajot = em.merge(oldAcheteurOfCajotListCajot);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Acheteur acheteur) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Acheteur persistentAcheteur = em.find(Acheteur.class, acheteur.getIdacheteur());
            List<Vente> venteListOld = persistentAcheteur.getVenteList();
            List<Vente> venteListNew = acheteur.getVenteList();
            List<Cajot> cajotListOld = persistentAcheteur.getCajotList();
            List<Cajot> cajotListNew = acheteur.getCajotList();
            List<String> illegalOrphanMessages = null;
            for (Cajot cajotListOldCajot : cajotListOld) {
                if (!cajotListNew.contains(cajotListOldCajot)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Cajot " + cajotListOldCajot + " since its acheteur field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Vente> attachedVenteListNew = new ArrayList<Vente>();
            for (Vente venteListNewVenteToAttach : venteListNew) {
                venteListNewVenteToAttach = em.getReference(venteListNewVenteToAttach.getClass(), venteListNewVenteToAttach.getIdvente());
                attachedVenteListNew.add(venteListNewVenteToAttach);
            }
            venteListNew = attachedVenteListNew;
            acheteur.setVenteList(venteListNew);
            List<Cajot> attachedCajotListNew = new ArrayList<Cajot>();
            for (Cajot cajotListNewCajotToAttach : cajotListNew) {
                cajotListNewCajotToAttach = em.getReference(cajotListNewCajotToAttach.getClass(), cajotListNewCajotToAttach.getIdcajot());
                attachedCajotListNew.add(cajotListNewCajotToAttach);
            }
            cajotListNew = attachedCajotListNew;
            acheteur.setCajotList(cajotListNew);
            acheteur = em.merge(acheteur);
            for (Vente venteListOldVente : venteListOld) {
                if (!venteListNew.contains(venteListOldVente)) {
                    venteListOldVente.setAcheteur(null);
                    venteListOldVente = em.merge(venteListOldVente);
                }
            }
            for (Vente venteListNewVente : venteListNew) {
                if (!venteListOld.contains(venteListNewVente)) {
                    Acheteur oldAcheteurOfVenteListNewVente = venteListNewVente.getAcheteur();
                    venteListNewVente.setAcheteur(acheteur);
                    venteListNewVente = em.merge(venteListNewVente);
                    if (oldAcheteurOfVenteListNewVente != null && !oldAcheteurOfVenteListNewVente.equals(acheteur)) {
                        oldAcheteurOfVenteListNewVente.getVenteList().remove(venteListNewVente);
                        oldAcheteurOfVenteListNewVente = em.merge(oldAcheteurOfVenteListNewVente);
                    }
                }
            }
            for (Cajot cajotListNewCajot : cajotListNew) {
                if (!cajotListOld.contains(cajotListNewCajot)) {
                    Acheteur oldAcheteurOfCajotListNewCajot = cajotListNewCajot.getAcheteur();
                    cajotListNewCajot.setAcheteur(acheteur);
                    cajotListNewCajot = em.merge(cajotListNewCajot);
                    if (oldAcheteurOfCajotListNewCajot != null && !oldAcheteurOfCajotListNewCajot.equals(acheteur)) {
                        oldAcheteurOfCajotListNewCajot.getCajotList().remove(cajotListNewCajot);
                        oldAcheteurOfCajotListNewCajot = em.merge(oldAcheteurOfCajotListNewCajot);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = acheteur.getIdacheteur();
                if (findAcheteur(id) == null) {
                    throw new NonexistentEntityException("The acheteur with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Acheteur acheteur;
            try {
                acheteur = em.getReference(Acheteur.class, id);
                acheteur.getIdacheteur();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The acheteur with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Cajot> cajotListOrphanCheck = acheteur.getCajotList();
            for (Cajot cajotListOrphanCheckCajot : cajotListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Acheteur (" + acheteur + ") cannot be destroyed since the Cajot " + cajotListOrphanCheckCajot + " in its cajotList field has a non-nullable acheteur field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Vente> venteList = acheteur.getVenteList();
            for (Vente venteListVente : venteList) {
                venteListVente.setAcheteur(null);
                venteListVente = em.merge(venteListVente);
            }
            em.remove(acheteur);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Acheteur> findAcheteurEntities() {
        return findAcheteurEntities(true, -1, -1);
    }

    public List<Acheteur> findAcheteurEntities(int maxResults, int firstResult) {
        return findAcheteurEntities(false, maxResults, firstResult);
    }

    private List<Acheteur> findAcheteurEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Acheteur.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Acheteur findAcheteur(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Acheteur.class, id);
        } finally {
            em.close();
        }
    }

    public int getAcheteurCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Acheteur> rt = cq.from(Acheteur.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
